package com.lipaluma.field.validator.impl.numeric;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.lipaluma.annotations.validations.ValidateInteger;
import com.lipaluma.context.Context;
import com.lipaluma.exceptions.CsvParserException;
import com.lipaluma.field.validator.ValidationError;
import com.lipaluma.field.validator.impl.generic.AbstractIsIncludedInValidator;
import com.lipaluma.xml.validator.XmlIncludedInValidator;
import com.lipaluma.xml.validator.XmlValidator;

/**
 * Validate a integer field to check that the value of the field must be included in at least one of different given values.<br/>
 * 
 * <p>
 * For Initialisation :
 * <ul>
 * 	<li>
 * 	<u><b>By annotations :</b></u> Use {@link ValidateInteger}, with the attribute <code>includeIn = {15, 17, 20}</code> to specify the list of
 * values accepted. <br/>
 * </li>
 * 	<li>
 * 	<u><b>By Xml :</b></u>
 * <ul>
 * This validator may be added using xml mapping with the element <code>includedIn</code> like below : <br/>
 * <code>
 * &lt;ToMap field="..."   ...><br/>
 * <b>
 * &nbsp;&nbsp;&nbsp;&lt;includedIn><br/>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;value>15&lt;/value><br/>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;value>17&lt;/value><br/>
 * &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;value>20&lt;/value><br/>
 * &nbsp;&nbsp;&nbsp;&lt;/includedIn>
 * </b><br/>
 * &lt;/ToMap>
 * </code>
 * </li>
 * </ul>
 * @author Mario
 *
 */
public class LIncludedInValidator extends AbstractIsIncludedInValidator<Long> {

	public LIncludedInValidator() {}
	public LIncludedInValidator(long... values) {
		if(values == null)
			throw new CsvParserException("Error on setting "+this.getClass()+" : given values can't be null");			
		List<Long> possibleNumbers = new ArrayList<Long>();
		for (Long number : values) {
			possibleNumbers.add(number);
		}
		init(possibleNumbers);
	}

	public LIncludedInValidator(int... values) {
		if(values == null)
			throw new CsvParserException("Error on setting "+this.getClass()+" : given values can't be null");			
		List<Long> possibleNumbers = new ArrayList<Long>();
		for (Integer number : values) {
			possibleNumbers.add(new Long(number));
		}
		init(possibleNumbers);
	}

	public boolean init(Context context, Field field) {
		ValidateInteger annotation = field.getAnnotation(ValidateInteger.class);
		if(annotation == null || annotation.includedIn().length == 0)
			return false;
		List<Long> possibleNumbers = new ArrayList<Long>();
		for (Long number : annotation.includedIn()) {
			possibleNumbers.add(number);
		}
		init(possibleNumbers);
		return true;
	}
	
	public boolean init(Context context, Field field, XmlValidator xmlValidator) {
		XmlIncludedInValidator validator = (XmlIncludedInValidator) xmlValidator;
		List<Long> possibleNumbers = new ArrayList<Long>();
		for(String string : validator.getValues()) {
			try {
				possibleNumbers.add(Long.parseLong(string)); 
			} catch(NumberFormatException ex) {
				throw new CsvParserException("Error on field "+field.getName()+" : An included value "+string+" can't be parsed to a numeric value", ex);
			}
		}
		init(possibleNumbers);
		return true;
	}

	@Override
	public boolean validate(Object value, List<ValidationError> errors) {
		return super.validate(new Long(value.toString()), errors);
	}
}
